package com.cooba.activitys;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

import com.cooba.R;
import com.cooba.views.MyWindowManager;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;


/**
 * Created by Administrator on 2015/1/11.
 * 服务详情页面
 */
public class BookTicketsActivity extends AppCompatActivity {
    private WebView web_dp;
    private String mMrl;
    public static BookTicketsActivity instance = null;
    private RelativeLayout cover_layer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_tickets);
        instance=this;
        PushAgent.getInstance(this).onAppStart();

        Intent intent = getIntent();
        mMrl = intent.getStringExtra("mMrl");
        web_dp = (WebView) findViewById(R.id.web_dp);
        cover_layer= (RelativeLayout) findViewById(R.id.cover_layer);
        cover_layer.getBackground().setAlpha(200);
        cover_layer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cover_layer.setVisibility(View.GONE);
                createView();
            }
        });
        init();
        //createView();
    }

    private void createView() {
       MyWindowManager.createMyWindow(getApplicationContext());
    }

    private void init() {
        WebSettings settings = web_dp.getSettings();
        settings.setJavaScriptCanOpenWindowsAutomatically(true);//支持通过JS打开新窗口
        settings.setJavaScriptEnabled(true);
        //启用数据库
        settings.setDatabaseEnabled(true);
        //设置定位的数据库路径
        String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        settings.setGeolocationDatabasePath(dir);
        //启用地理定位
        settings.setGeolocationEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAppCacheEnabled(true);//是否使用缓存
        settings.setTextSize(WebSettings.TextSize.NORMAL);
        settings.setDefaultTextEncodingName("UTF-8");
        //web_dp.loadUrl("http://m.ctrip.com");
        //配置权限
        web_dp.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedIcon(WebView view, Bitmap icon) {
                super.onReceivedIcon(view, icon);
            }

            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
                super.onGeolocationPermissionsShowPrompt(origin, callback);
            }
        });


        // web_dp.loadDataWithBaseURL(mMrl, null, "text/html", "utf-8", null);
        web_dp.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });
        web_dp.loadUrl(mMrl);
        web_dp.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
               // createView();
            }
        });
    }

    @Override
    // 设置回退
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && web_dp.canGoBack()) {
            web_dp.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        MyWindowManager.removeMyWindow(getApplicationContext());
        super.onDestroy();
    }


}
